extern "C" {
#include <gst/gst.h>
#include <gst/rtsp-server/rtsp-server.h>
#include <gst/rtsp-server/rtsp-session-pool.h>
}

#if 0
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    guint major, minor, micro, nano;
    /* Initialize GStreamer */
    gst_init (&argc, &argv);

    gst_version(&major, &minor, &micro, &nano);

    qDebug("Gstreamer %d.%d.%d_%d", major, minor, micro, nano);

    return a.exec();
}
#endif



static gboolean
timeout (GstRTSPServer * server)
{
  GstRTSPSessionPool *pool;

  pool = gst_rtsp_server_get_session_pool (server);

  gst_rtsp_session_pool_cleanup (pool);
  g_object_unref (pool);

  return TRUE;
}

int
main (int argc, char *argv[])
{
  GMainLoop *loop;
  GstRTSPServer *server;
  GstRTSPMountPoints *mounts;
  GstRTSPMediaFactory *factory;
  GstRTSPMediaFactory *factory1;
  GstRTSPSessionPool *session;

  gst_init (&argc, &argv);

  loop = g_main_loop_new (NULL, FALSE);

  session = gst_rtsp_session_pool_new();
  gst_rtsp_session_pool_set_max_sessions  (session, 255);
  /* 创建服务器实例 */
  server = gst_rtsp_server_new ();

  /* 获取服务器的rtsp流的管理器*/
  mounts = gst_rtsp_server_get_mount_points (server);

  /* 创建两个rtsp的流管理器，设置流的源*/
  factory = gst_rtsp_media_factory_new ();
  factory1 = gst_rtsp_media_factory_new ();

#ifdef OE_CROSS_PLATFORM
#if 1
  gst_rtsp_media_factory_set_launch (factory,
                                     "( "
                                     "filesrc location=/media/disk/my.mp3 ! id3demux ! audioconvert ! audioresample ! imxmp3enc name=d "
                                     "d. ! queue ! rtpmpapay name=pay0 pt=96 " ")");
#else
  gst_rtsp_media_factory_set_launch (factory,
                                     "( "
                                     "souphttpsrc location=http://fdfs.xmcdn.com/group34/M03/5D/9D/wKgJYVnzAGiT0qpyACcbm4jsxs8514.mp3 ! mad ! audioconvert ! audioresample ! lamemp3enc name=d "
                                     "d. ! queue ! rtpmpapay name=pay0 pt=96 " ")");
#endif


  gst_rtsp_media_factory_set_launch (factory1,
                                     "( "
                                     "filesrc location=/media/disk/my.mp4 ! qtdemux name=d "
                                     "d. ! queue ! rtph264pay pt=96 name=pay0 "
                                     "d. ! queue ! rtpmp4apay pt=97 name=pay1 " ")");
#else

#if 0
  gst_rtsp_media_factory_set_launch (factory,
                                     "( "
                                     "filesrc location=/home/help/Videos/my.mp3 ! mad ! audioconvert ! audioresample ! lamemp3enc name=d "
                                     "d. ! queue ! rtpmpapay name=pay0 pt=96 " ")");
#else
  gst_rtsp_media_factory_set_launch (factory,
                                     "( "
                                     "souphttpsrc location=http://fdfs.xmcdn.com/group34/M03/5D/9D/wKgJYVnzAGiT0qpyACcbm4jsxs8514.mp3 ! mad ! audioconvert ! audioresample ! lamemp3enc name=d "
                                     "d. ! queue ! rtpmpapay name=pay0 pt=96 " ")");
#endif

  gst_rtsp_media_factory_set_launch (factory1,
                                     "( "
                                     "filesrc location=/home/help/Videos/my.mp4 ! qtdemux name=d "
                                     "d. ! queue ! rtph264pay pt=96 name=pay0 "
                                     "d. ! queue ! rtpmp4apay pt=97 name=pay1 " ")");
#endif

  gst_rtsp_media_factory_set_shared (factory, TRUE);
  gst_rtsp_media_factory_set_shared (factory1, TRUE);

  /*绑定流的地址，并加入流管理器中*/
  gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
  gst_rtsp_mount_points_add_factory (mounts, "/test1", factory1);

  g_object_unref (mounts);

  gst_rtsp_server_attach (server, NULL);

  g_timeout_add_seconds (2, (GSourceFunc) timeout, server);

  g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
  g_print ("stream ready at rtsp://127.0.0.1:8554/test1\n");
  g_main_loop_run (loop);

  return 0;
}
